import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;



/**
 * The Class report repeated words in a file
 */
public class ReportRepeatedWordsDriver {
	
	private FileInputStream fInputStream;
	private FileWriter fOutputStream;
	private BufferedWriter bufferOut;
	private char[] charBuffer; //save text from Input.txt
	private String[] strBuffer; //save words from charBuffer
	private int charBufferlength = 0; //count charBuffer length
	private int strBufferlength = 0; //count strBufferlength
	private char[] wordDistinguisher = {'\0', '\n', '	', ' ', ',', '.', '!', '-', '+', '=', ';', ':', '?', '&', '{', '}', '\\'};
	
	
	/**
	 * Instantiates a new report repeated words driver.
	 *
	 * @param fName the f name
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public ReportRepeatedWordsDriver(String fName) throws IOException{
		fInputStream = null;
		fInputStream = new FileInputStream(fName);		
	}
	
	/**
	 * This method reads a file and copies text from the file to charBuffer  
	 *
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public void readFileAndPasteToBuffer() throws IOException{
		
		charBuffer = new char[1024];
		
		while(true){
			int aChar = fInputStream.read(); //read a character from a file
			if(aChar == -1){
				break;
			}			
			charBuffer[charBufferlength++] =  (char)aChar; //save characters to charBuffer		
		}		
	}
	
	/**
	 * This method Copies text from charBuffer to strBuffer.
	 */
	public void copyToStringBuffer(){
		
		int FirstOfWordOffest = 0;
		strBuffer = new String[1024];
		
		for(int i = 0; i <= charBufferlength; i++){
			
			//save each word to strBuffer without word distinguisher, such as '\0', '\n', '	', ' ', ',', '.', '!', '-', '+', '=', ';', ':', '?', '&', '{', '}', '\\'
			if(compareCharAndwordDistinguisher(charBuffer[i])){
				strBuffer[strBufferlength++] = String.copyValueOf(charBuffer, FirstOfWordOffest,i-FirstOfWordOffest);
				while(true){
					if(!compareCharAndwordDistinguisher(charBuffer[i])||charBuffer[i]=='\0'){
						break;
					}else{
						i++;
					}
				}
				FirstOfWordOffest = i; //move a word offset that directs at the first of word in charBuffer[] 
			}

		}
		
		for(int i = 0; i < strBufferlength; i++)
			System.out.println(i+" :"+strBuffer[i]);
		
	}
	
	/**
	 * Compare a character and word distinguishers['\0', '\n', '	', ' ', ',', '.', '!', '-', '+', '=', ';', ':', '?', '&', '{', '}', '\\'.]
	 * if the character c is a word distinguisher, return true;
	 * @param c the c
	 * @return true, if successful
	 */
	public boolean compareCharAndwordDistinguisher(char c){
		int lengthOfWordDistinguisher = 17;
		
		for(int i = 0; i < lengthOfWordDistinguisher; i++){
			if(c == wordDistinguisher[i])
				return true;
		}
		return false;
	}
	
	/**
	 * This method compares all words in strBuffer and write repeated words to Output.txt
	 * @throws IOException 
	 */
	public void CompareRepeatedWord() throws IOException{
		fOutputStream = new FileWriter("Output.txt");
		bufferOut = new BufferedWriter(fOutputStream);
		
		int lineCounter = 1; //Count line number
		for(int i = 0; i < strBufferlength-1; i++){
			if(strBuffer[i+1].equals("\r")){  
				lineCounter++;
				//if strBuffer[i+1] is "\r", increase lineCounter by 1 and compare strBuffer[i+1] and strBuffer[i+2]
				if(strBuffer[i].equals(strBuffer[i+2])){
					System.out.println("Repeated word on line "+lineCounter+":"+strBuffer[i]+" "+strBuffer[i+2]);
					bufferOut.write("Repeated word on line "+lineCounter+":"+strBuffer[i]+" "+strBuffer[i+1]+" \r\n");
				}
				continue;
			}

			
			if(strBuffer[i].equals(strBuffer[i+1])){
				System.out.println("Repeated word on line "+lineCounter+":"+strBuffer[i]+" "+strBuffer[i+1]);
				bufferOut.write("Repeated word on line "+lineCounter+":"+strBuffer[i]+" "+strBuffer[i+1]+" \r\n");
			}
		}
		
		bufferOut.close();
		fOutputStream.close();
	}
}
